package com.scnu409.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.scnu409.fragment.HistoryContentFragment.Callbacks;
import com.scnu409.mimicontact.R;
import com.scnu409.mimicontact.UserInf;
import com.scnu409.utils.Person;
import com.scnu409.utils.TestContent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class CommunityFragment extends Fragment {
	private Callbacksd mCallbacks;

	/*
	 * 此Fragment所在的activity必须实现这个接口
	 */
	public interface Callbacksd {
		public void onItemSelected(Integer id);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		// 如果没有实现这个接口，抛出异常
		if (!(activity instanceof Callbacksd)) {
			throw new IllegalStateException("hehe");
		}
		mCallbacks = (Callbacksd) activity;
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mCallbacks = null;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.community, container, false);
		com.scnu409.mimicontact.RefreshListView listView = (com.scnu409.mimicontact.RefreshListView) view
				.findViewById(R.id.refreshList);
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < TestContent._name.length; i++) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("name", TestContent._name[i]);
			item.put("img", TestContent._simpleImage[i]);
			item.put("content", TestContent._content[i]);
			item.put("img_view", R.drawable.test);
			list.add(item);
		}

		// 实现Adapter
		SimpleAdapter adapter = new SimpleAdapter(getActivity(), list,
				R.layout.community_content, new String[] { "name", "img",
						"content", "img_view" }, new int[] { R.id.user_name,
						R.id.user_img, R.id.content, R.id.img_view });

		listView.setAdapter(adapter);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
}
